<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\matrix\controller; //Demo插件英文名，改成你的插件英文就行了
use cmf\controller\PluginMatrixRestBaseController;
use think\Db;

class ApiIndexController extends PluginMatrixRestBaseController
{

    /**
     * 构造函数
     */
    public function _initialize()
    {
        if (empty($this->user)) {
            //$this->error(['code' => 10000, 'msg' => '未登录']);
        }
    }
    /**
     * 群消息列表
     */
    public function query_msg_text_list()
    {
        $page = $this->request->param("page");
        $talker = $this->request->param("talker");
        $content = $this->request->param("content");
        if(empty($page)){
            $page = 1;
        }
        $listRows = 15;

        $sql = "SELECT count(*) totalCount FROM bot_matrix_msg_text where 1 = 1 ";
        $sql .= empty($talker) ? "" : " and talker = '".$talker."' ";
        $sql .= empty($content) ? "" : " and content like '%".$content."content' ";
        $totalCount = Db::query($sql);

        $msgsql = "SELECT talker,sender,content,create_time FROM bot_matrix_msg_text where 1 = 1 ";
        $msgsql .= empty($talker) ? "" : " and talker = '".$talker."' ";
        $msgsql .= empty($content) ? "" : " and content like '%".$content."content' ";
        $msgsql .= " ORDER BY create_time DESC LIMIT ".($page-1)*$listRows.",".$listRows;

        $charroomsql = "SELECT r.talker,r.talker_name,t.sender,t.content,t.create_time FROM  bot_matrix_charroom r Right JOIN (";
        $charroomsql .= $msgsql;
        $charroomsql .= ") t ON r.talker = t.talker";

        $sql = "SELECT msg.talker,msg.talker_name,member.nickname,msg.sender,msg.content,msg.create_time FROM  bot_matrix_member member Right JOIN (";
        $sql .= $charroomsql;
        $sql .= ") msg on msg.sender = member.username";
        var_dump($sql);
        $list = Db::query($sql);
        if(count($list) > 0){
            foreach ($list as $key => &$val) {
                $val['create_time'] = date("Y-m-d h:i",$val['create_time']);
            }
            $this->success('success',array("total_count"=>$totalCount[0]['totalCount'],"list"=>$list));
        }else{
            $this->successcode(2,"暂无数据");
        }
    }

    /**
     * 群视频预警列表
     */
    public function query_msg_video_list()
    {
        $page = $this->request->param("page");
        $talker = $this->request->param("talker");
        if(empty($page)){
            $page = 1;
        }
        $listRows = 15;

        $sql = "SELECT count(*) totalCount FROM bot_matrix_msg_video where 1 = 1 ";
        $sql .= empty($talker) ? "" : " and talker = '".$talker."' ";
        $totalCount = Db::query($sql);

        $msgsql = "SELECT talker,sender,create_time FROM bot_matrix_msg_video where 1 = 1 ";
        $msgsql .= empty($talker) ? "" : " and talker = '".$talker."' ";
        $msgsql .= " ORDER BY msg_svr_id DESC LIMIT ".($page-1)*$listRows.",".$listRows;

        $charroomsql = "SELECT r.talker,r.talker_name,t.sender,t.create_time FROM  bot_matrix_charroom r Right JOIN (";
        $charroomsql .= $msgsql;
        $charroomsql .= ") t ON r.talker = t.talker";

        $sql = "SELECT msg.talker,msg.talker_name,member.nickname,msg.sender,msg.create_time FROM  bot_matrix_member member Right JOIN (";
        $sql .= $charroomsql;
        $sql .= ") msg on msg.sender = member.username";

        $list = Db::query($sql);
        if(count($list) > 0){
            foreach ($list as $key => &$val) {
                $val['create_time'] = date("Y-m-d h:i",$val['create_time']);
            }
            $this->success('success',array("total_count"=>$totalCount[0]['totalCount'],"list"=>$list));
        }else{
            $this->successcode(2,"暂无数据");
        }
    }

    /**
     * 入群记录
     */
    public function query_msg_addgroup_list()
    {
        $page = $this->request->param("page");
        $talker = $this->request->param("talker");
        if(empty($page)){
            $page = 1;
        }
        $listRows = 15;

        $sql = "SELECT count(*) totalCount FROM bot_matrix_msg_addgroup where 1 = 1 ";
        $sql .= empty($talker) ? "" : " and talker = '".$talker."' ";
        $totalCount = Db::query($sql);

        $msgsql = "SELECT talker,sender,nickname,create_time FROM bot_matrix_msg_addgroup where 1 = 1 ";
        $msgsql .= empty($talker) ? "" : " and talker = '".$talker."' ";
        $msgsql .= " ORDER BY msg_svr_id DESC LIMIT ".($page-1)*$listRows.",".$listRows;

        $charroomsql = "SELECT r.talker,r.talker_name,t.sender,t.nickname,t.create_time FROM  bot_matrix_charroom r Right JOIN (";
        $charroomsql .= $msgsql;
        $charroomsql .= ") t ON r.talker = t.talker";



        $list = Db::query($charroomsql);
        if(count($list) > 0){
            foreach ($list as $key => &$val) {
                $val['create_time'] = date("Y-m-d h:i",$val['create_time']);
            }
            $this->success('success',array("total_count"=>$totalCount[0]['totalCount'],"list"=>$list));
        }else{
            $this->successcode(2,"暂无数据");
        }
    }



}
